package useraddress

import (
	"context"
	"strconv"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/user/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserAddressListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取UserAddress列表
func NewGetUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserAddressListLogic {
	return &GetUserAddressListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserAddressListLogic) GetUserAddressList(req *types.UserAddressListReq) (resp *types.UserAddressListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var ids *string
	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}

	data, err := l.svcCtx.UserRpc.BackendGetUserAddressList(l.ctx, &userclient.UserAddressListReq{
		Page:      page,
		PageSize:  pageSize,
		UserId:    req.UserId,
		Ids:       ids,
		Consignee: req.Consignee,
		Mobile:    req.Mobile,
		Status:    req.Status,
		OrderBy:   orderBys,
		FilterBy:  filterBys,
	})
	if err != nil {
		return
	}

	resp = &types.UserAddressListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.UserAddressListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}
	for _, v := range data.List {
		var geographic []*types.UserMetaInfo
		if len(v.Geographic) > 0 {
			for _, item := range v.Geographic {
				geographic = append(geographic, &types.UserMetaInfo{
					Uid:   item.Uid,
					Id:    item.Id,
					Key:   item.Key,
					Value: item.Value,
					Label: item.Label,
					Slug:  item.Slug,
				})
			}
		}
		option := utils.GetOptionByStatus(v.Status)

		resp.Data.List = append(resp.Data.List, &types.UserAddressInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Label:           v.Label,
			UserId:          v.UserId,
			Consignee:       v.Consignee,
			Mobile:          v.Mobile,
			Province:        v.Province,
			City:            v.City,
			District:        v.District,
			Street:          v.Street,
			DetailedAddress: v.DetailedAddress,
			PostalCode:      v.PostalCode,
			RegionCode:      v.RegionCode,
			IsDefault:       v.IsDefault,
			Status:          v.Status,
			LastUsedAt:      v.LastUsedAt,
			Geographic:      geographic,
		})
	}

	return resp, nil
}
